import os
import numpy as np

def load_word_vecs():
	embeddings_index = {}
	f = open(os.path.dirname(os.path.abspath(__file__))+'/sgns.baidubaike.bigram-char', 'r',encoding='utf8')
	f.readline()#escape first line
	for line in f:
	    values = line.split(maxsplit=1)
	    word = values[0]
	    coefs = np.asarray(values[1:], dtype='float32')
	    embeddings_index[word] = coefs
	f.close()

	print('Found %s word vectors.' % len(embeddings_index))
	return embeddings_index

def load_word_vecs_1():
    embeddings_index = {}
    file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sgns.baidubaike.bigram-char')

    with open(file_path, 'r', encoding='utf8') as f:
        # 跳过首行（标题行）
        next(f)

        valid_vectors = 0
        error_lines = 0

        # 从第二行开始计数
        for line_num, line in enumerate(f, start=2):
            line = line.strip()
            if not line:
                continue

            # 安全分割：最多分割一次（处理词中含空格的情况）
            parts = line.split(maxsplit=1)
            if len(parts) != 2:
                print(f"格式错误 - 第 {line_num} 行: 列数不足 | 内容: {line[:50]}...")
                error_lines += 1
                continue

            word, vec_str = parts

            try:
                # 更安全的数值转换方式
                coefs = np.fromstring(vec_str, dtype='float32', sep=' ')
                # 验证向量长度一致性（可选）
                if valid_vectors == 0:
                    vec_dim = coefs.shape
                elif coefs.shape != vec_dim:
                    print(f"维度异常 - 第 {line_num} 行: 预期维度 {vec_dim}，实际维度 {coefs.shape}")
                    error_lines += 1
                    continue

                embeddings_index[word] = coefs
                valid_vectors += 1
            except ValueError as e:
                print(f"转换失败 - 第 {line_num} 行: {str(e)} | 内容: {vec_str[:50]}...")
                error_lines += 1
                continue

    print(f'成功加载词向量: {valid_vectors:,} 条')
    print(f'跳过异常行: {error_lines} 条')
    return embeddings_index


def load_word_vecs_2():
	embeddings = {}
	file_path = os.path.join(os.path.dirname(__file__), 'sgns.baidubaike.bigram-char')

	with open(file_path, 'r', encoding='utf8') as f:
		next(f)  # 跳过首行

		for line in f:
			parts = line.strip().split(maxsplit=1)
			if len(parts) != 2:
				continue

			word, vec_str = parts
			try:
				embeddings[word] = np.fromstring(vec_str, dtype='float32', sep=' ')
			except:
				continue

	print(f'Loaded {len(embeddings)} vectors')
	return embeddings